package com.ubis.ibms.plugin.composites.worker;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

/**
 * 业主信息
 * @author diamond-wang
 *
 */
public class WorkerInfoComposite extends Composite {
	//编辑按钮
	private Button editButton;
	// 姓名
	private Text nameText;
	// 性别
	private Combo genderCombo;
	// 联系电话
	private Text telText;
	//部门
	private ComboViewer depComboViewer;
	private Combo depCombo;
	//职务
	private ComboViewer posComboViewer;
	private Combo posCombo;
	//入职时间
	private CalendarCombo entryCombo;
	//标志变量，true时为浏览状态，false为编辑状态
	private boolean read=true;
	//考勤信息表
	private TableViewer attendTableViewer;
	private Table attendTable;
	//工作信息表
	private TableViewer workTableViewer;
	private Table workTable;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public WorkerInfoComposite(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		setLayout(layout);
		
		Composite basicComposite = new Composite(parent, SWT.NONE);
		basicComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		basicComposite.setLayout(new GridLayout(6, false));
		
		Label title = new Label(basicComposite, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		title.setText("基本信息");
		new Label(basicComposite, SWT.NONE);
		new Label(basicComposite, SWT.NONE);
		new Label(basicComposite, SWT.NONE);
		new Label(basicComposite, SWT.NONE);
		
		editButton = new Button(basicComposite, SWT.NONE);
		editButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		editButton.setText("编辑");
		editButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if(read){//浏览状态下
					//按钮为编辑，点击变为编辑状态布局，按钮变为“保存”
					toEdit();
				}
				else{//编辑状态下
					//按钮为保存，点击校验、保存信息，按钮变为“编辑”
					//TODO 校验、保存信息
					toRead();
				}
			}
		});
		
		// 姓名
		Label nameLabel = new Label(basicComposite, SWT.RIGHT);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel.setText("姓名：");
		nameText = new Text(basicComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		nameText.setEditable(false);

		// 性别
		Label genderLabel = new Label(basicComposite, SWT.RIGHT);
		genderLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		genderLabel.setText("性别：");
		genderCombo = new Combo(basicComposite, SWT.NONE);
		genderCombo.setItems(new String[] {"男", "女"});
		genderCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		//联系电话
		Label telLabel = new Label(basicComposite, SWT.RIGHT);
		telLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		telLabel.setText("联系电话：");
		telText = new Text(basicComposite, SWT.BORDER);
		telText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		// 部门
		Label depLabel = new Label(basicComposite, SWT.RIGHT);
		depLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		depLabel.setText("部门：");
		depComboViewer = new ComboViewer(basicComposite, SWT.READ_ONLY);
		depCombo = depComboViewer.getCombo();
		depCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		depComboViewer.setContentProvider(ArrayContentProvider
				.getInstance());
		depComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				//TODO 得到部门text
//						if (element instanceof Building) {
//							Building building = (Building) element;
//							return building.getEms_name();
//						}
				return super.getText(element);
			}
		});
		// TODO 获取部门数据
		
		// 职务
		Label posLabel = new Label(basicComposite, SWT.RIGHT);
		posLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		posLabel.setText("职务：");
		posComboViewer = new ComboViewer(basicComposite, SWT.READ_ONLY);
		posCombo = posComboViewer.getCombo();
		posCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		posComboViewer.setContentProvider(ArrayContentProvider
				.getInstance());
		posComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				//TODO 得到职务text
				return super.getText(element);
			}
		});
		// TODO 获取职务数据
		
		// 入职时间
		Label entryLabel = new Label(basicComposite, SWT.RIGHT);
		entryLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		entryLabel.setText("入职时间：");
		entryCombo = new CalendarCombo(basicComposite, SWT.BORDER);
		entryCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		entryCombo.setEnabled(false);
		
		Label attendLabel = new Label(parent, SWT.NONE);
		attendLabel.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		attendLabel.setText("考勤信息");
		
		attendTableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
		attendTable = attendTableViewer.getTable();
		attendTable.setLinesVisible(true);
		attendTable.setHeaderVisible(true);
		attendTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		TableViewerColumn attendSeqViewerColumn = new TableViewerColumn(attendTableViewer, SWT.NONE);
		TableColumn attendSeqTableColumn = attendSeqViewerColumn.getColumn();
		attendSeqTableColumn.setResizable(false);
		attendSeqTableColumn.setAlignment(SWT.CENTER);
		attendSeqTableColumn.setWidth(40);
		attendSeqTableColumn.setText("序号");
		
		TableViewerColumn attendYearViewerColumn = new TableViewerColumn(attendTableViewer, SWT.NONE);
		TableColumn attendYearTableColumn = attendYearViewerColumn.getColumn();
		attendYearTableColumn.setResizable(false);
		attendYearTableColumn.setAlignment(SWT.CENTER);
		attendYearTableColumn.setWidth(50);
		attendYearTableColumn.setText("年份");
		
		TableViewerColumn attendMonthViewerColumn = new TableViewerColumn(attendTableViewer, SWT.NONE);
		TableColumn attendMonthTableColumn = attendMonthViewerColumn.getColumn();
		attendMonthTableColumn.setResizable(false);
		attendMonthTableColumn.setAlignment(SWT.CENTER);
		attendMonthTableColumn.setWidth(50);
		attendMonthTableColumn.setText("月份");
		
		TableViewerColumn attendSumViewerColumn = new TableViewerColumn(attendTableViewer, SWT.NONE);
		TableColumn attendSumTableColumn = attendSumViewerColumn.getColumn();
		attendSumTableColumn.setResizable(false);
		attendSumTableColumn.setAlignment(SWT.CENTER);
		attendSumTableColumn.setWidth(50);
		attendSumTableColumn.setText("总天数");
		
		TableViewerColumn attendWorkDaysViewerColumn = new TableViewerColumn(attendTableViewer, SWT.NONE);
		TableColumn attendWorkDaysTableColumn = attendWorkDaysViewerColumn.getColumn();
		attendWorkDaysTableColumn.setResizable(false);
		attendWorkDaysTableColumn.setAlignment(SWT.CENTER);
		attendWorkDaysTableColumn.setWidth(60);
		attendWorkDaysTableColumn.setText("工作天数");
		
		TableViewerColumn attendVocationDaysViewerColumn = new TableViewerColumn(attendTableViewer, SWT.NONE);
		TableColumn attendVocationDaysTableColumn = attendVocationDaysViewerColumn.getColumn();
		attendVocationDaysTableColumn.setResizable(false);
		attendVocationDaysTableColumn.setAlignment(SWT.CENTER);
		attendVocationDaysTableColumn.setWidth(60);
		attendVocationDaysTableColumn.setText("休假天数");
		
		TableViewerColumn attendOffDaysViewerColumn = new TableViewerColumn(attendTableViewer, SWT.NONE);
		TableColumn attendOffDaysTableColumn = attendOffDaysViewerColumn.getColumn();
		attendOffDaysTableColumn.setResizable(false);
		attendOffDaysTableColumn.setAlignment(SWT.CENTER);
		attendOffDaysTableColumn.setWidth(60);
		attendOffDaysTableColumn.setText("旷工天数");
		
		TableViewerColumn attendBreakDaysViewerColumn = new TableViewerColumn(attendTableViewer, SWT.NONE);
		TableColumn attendBreakDaysTableColumn = attendBreakDaysViewerColumn.getColumn();
		attendBreakDaysTableColumn.setResizable(false);
		attendBreakDaysTableColumn.setAlignment(SWT.CENTER);
		attendBreakDaysTableColumn.setWidth(60);
		attendBreakDaysTableColumn.setText("违规天数");
		
		Label workLabel = new Label(parent, SWT.NONE);
		workLabel.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		workLabel.setText("工作信息");
		
		workTableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
		workTable = workTableViewer.getTable();
		workTable.setLinesVisible(true);
		workTable.setHeaderVisible(true);
		workTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		TableViewerColumn workSeqViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn workSeqTableColumn = workSeqViewerColumn.getColumn();
		workSeqTableColumn.setResizable(false);
		workSeqTableColumn.setAlignment(SWT.CENTER);
		workSeqTableColumn.setWidth(40);
		workSeqTableColumn.setText("序号");
		
		TableViewerColumn workYearViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn workYearTableColumn = workYearViewerColumn.getColumn();
		workYearTableColumn.setResizable(false);
		workYearTableColumn.setAlignment(SWT.CENTER);
		workYearTableColumn.setWidth(50);
		workYearTableColumn.setText("年份");
		
		TableViewerColumn workMonthViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn workMonthTableColumn = workMonthViewerColumn.getColumn();
		workMonthTableColumn.setResizable(false);
		workMonthTableColumn.setAlignment(SWT.CENTER);
		workMonthTableColumn.setWidth(50);
		workMonthTableColumn.setText("月份");
		
		TableViewerColumn workSumViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn workSumTableColumn = workSumViewerColumn.getColumn();
		workSumTableColumn.setResizable(false);
		workSumTableColumn.setAlignment(SWT.CENTER);
		workSumTableColumn.setWidth(50);
		workSumTableColumn.setText("总天数");
		
		TableViewerColumn workDaysViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn workDaysTableColumn = workDaysViewerColumn.getColumn();
		workDaysTableColumn.setResizable(false);
		workDaysTableColumn.setAlignment(SWT.CENTER);
		workDaysTableColumn.setWidth(60);
		workDaysTableColumn.setText("工作天数");
		
		TableViewerColumn workAmountViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn workAmountTableColumn = workAmountViewerColumn.getColumn();
		workAmountTableColumn.setResizable(false);
		workAmountTableColumn.setAlignment(SWT.CENTER);
		workAmountTableColumn.setWidth(60);
		workAmountTableColumn.setText("总工作数");
		
		TableViewerColumn finishAmountViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn finishAmountTableColumn = finishAmountViewerColumn.getColumn();
		finishAmountTableColumn.setResizable(false);
		finishAmountTableColumn.setAlignment(SWT.CENTER);
		finishAmountTableColumn.setWidth(100);
		finishAmountTableColumn.setText("完成工作数");
		
		TableViewerColumn goodAmountViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn goodAmountTableColumn = goodAmountViewerColumn.getColumn();
		goodAmountTableColumn.setResizable(false);
		goodAmountTableColumn.setAlignment(SWT.CENTER);
		goodAmountTableColumn.setWidth(100);
		goodAmountTableColumn.setText("优秀工作数");
		
		TableViewerColumn passAmountViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn passAmountTableColumn = passAmountViewerColumn.getColumn();
		passAmountTableColumn.setResizable(false);
		passAmountTableColumn.setAlignment(SWT.CENTER);
		passAmountTableColumn.setWidth(100);
		passAmountTableColumn.setText("合格工作数");
		
		TableViewerColumn failAmountViewerColumn = new TableViewerColumn(workTableViewer, SWT.NONE);
		TableColumn failAmountTableColumn = failAmountViewerColumn.getColumn();
		failAmountTableColumn.setResizable(false);
		failAmountTableColumn.setAlignment(SWT.CENTER);
		failAmountTableColumn.setWidth(100);
		failAmountTableColumn.setText("不合格工作数");
		
		toRead();
	}

	@Override
	protected void checkSubclass() {
	}
	
	/**
	 * 变为浏览状态
	 */
	public void toRead(){
		genderCombo.setEnabled(false);
		telText.setEditable(false);
		depCombo.setEnabled(false);
		posCombo.setEnabled(false);
		read=true;
		editButton.setText("编辑");
	}
	
	/**
	 * 变为编辑状态
	 */
	public void toEdit(){
		genderCombo.setEnabled(true);
		telText.setEditable(true);
		depCombo.setEnabled(true);
		posCombo.setEnabled(true);
		read=false;
		editButton.setText("保存");
	}
}
